package pers.vinsonws.jianghu.serverspring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vinsonws.jianghu.serverspring.entity.ApplyChatRooms;
import pers.vinsonws.jianghu.serverspring.entity.Users;
import pers.vinsonws.jianghu.serverspring.repositories.ApplyChatRoomRepository;

import java.util.Set;

/**
 * @auther vinsonws
 * @date 2018/11/6 22:08
 */
@Service
public class ApplyChatRoomServiceImpl implements ApplyChatRoomService {
    @Autowired
    ApplyChatRoomRepository applyChatRoomRepository;
    @Override
    public boolean saveApplyRequest(ApplyChatRooms applyChatRooms) {
        ApplyChatRooms applyChatRooms1 = applyChatRoomRepository.findByChatRoomIdAndUserId(
                applyChatRooms.getChatRoomId(), applyChatRooms.getUserId());
        if (applyChatRooms1!= null){
            if (applyChatRooms1.isStatus()){
                applyChatRooms1.setStatus(false);
                return applyChatRoomRepository.saveAndFlush(applyChatRooms1) != null;
            }

        } else {
            return applyChatRoomRepository.save(applyChatRooms) != null;
        }
        return false;
    }
    @Override
    public Set<ApplyChatRooms> getApplyByUserId(int userId){
        return applyChatRoomRepository.findAllByUserId(userId);
    }

    @Override
    public Set<ApplyChatRooms> getApplyByChatRoomId(int chatRoomId) {
        return applyChatRoomRepository.findAllByChatRoomId(chatRoomId);
    }

    @Override
    public boolean operateApply(int chatRoomId, int userId, boolean pass) {
        ApplyChatRooms applyChatRooms = applyChatRoomRepository.findByChatRoomIdAndUserId(chatRoomId, userId);
        applyChatRooms.setStatus(true);
        applyChatRooms.setPass(pass);

        return applyChatRoomRepository.saveAndFlush(applyChatRooms) != null;
    }
}
